/***********************************************
 This file is part of redia-bapps-main-ios.
 
 Copyright (c) 2015 Redia A/S
 
 Redia-bapps-main-ios is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Redia-bapps-main-ios is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with redia-bapps-main-ios.  If not, see <http://www.gnu.org/licenses/>.
 
 *********************************************** */

//
//  BAAboutAuthorViewController.m
//  redia-bapps-ios
//
//  Created by Mads Darø Kristensen on 10/24/13.
//  Copyright (c) 2013 redia. All rights reserved.
//

#import "BAAboutAuthorViewController.h"
#import "BAUtils.h"
#import "BAAboutCreator.h"
#import "BALibrarySettings.h"
#import "UIImage+MultiplyColor.h"
#import "BAWebBrowserViewController.h"
#import "BACustomNavigationBar.h"

@interface BAAboutAuthorViewController ()

@end

@implementation BAAboutAuthorViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    RD_SET_EDGES_FOR_EXTENDED_LAYOUT_IOS7
    self.view.backgroundColor = BA_CUSTOM_NAVBAR_DEFAULT_GRAY_COLOR;
    self.linkView.backgroundColor = BA_CUSTOM_NAVBAR_DEFAULT_GRAY_COLOR;
    
    if (RD_IS_IPAD) {
        self.headerLabel.font = BA_FONT_LIGHT(16);
        self.authorNameLabel.font = BA_FONT_MEDIUM(18);
        self.linkTitleLabel.font = BA_FONT_MEDIUM(16);
    } else {
        BA_LABEL_SET_FONT_LIGHT(self.headerLabel);
        BA_LABEL_SET_FONT_MEDIUM(self.authorNameLabel);
        BA_LABEL_SET_FONT_MEDIUM(self.linkTitleLabel);
    }
    
    self.globeImageView.image = [UIImage imageNamed:@"globe-white" multiplyWithColor:[[BALibrarySettings instance] getCustomerColorNormal]];
    self.contentWebView.scrollView.bounces = NO;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(linkViewTapped)];
    [self.linkView addGestureRecognizer:tapGesture];
    
    if (self.relatedMaterialsData) {
        [self populateView];
    }
}

- (void)populateView
{
    NSString* maincontent = @"";
    
    if (self.relatedMaterialsData.relationType == BA_RELATED_MATERIAL_TYPE_ABOUT_AUTHOR) {
        self.authorNameLabel.text = self.relatedMaterialsData.aboutAuthor.name;
        maincontent = self.relatedMaterialsData.aboutAuthor.abstract;

    } else if (self.relatedMaterialsData.relationType == BA_RELATED_MATERIAL_TYPE_SINGLE_REVIEW) {
        self.headerLabel.text = @"Anmeldelse";
        self.authorNameLabel.text = self.relatedMaterialsData.singleReview.reviewCreator;
        self.bottomButtonHeightConstraint.constant = 0;
        NSString* maintext = @"";
        if (self.relatedMaterialsData.singleReview.text!=nil) {
            maintext = [self.relatedMaterialsData.singleReview.text stringByReplacingOccurrencesOfString:@"\n" withString:@"</p><p>"];
        }
        maincontent = [NSString stringWithFormat:@"<p><b>%@</b><br /><i>%@</i></p><p>%@</p>",
                       self.relatedMaterialsData.singleReview.title,
                       self.relatedMaterialsData.singleReview.reviewCreator,
                       maintext
                       
                       ];
    }
    
    NSString *css = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:RD_IS_IPAD ? @"BAAboutAuthorTemplate_iPad" : @"BAAboutAuthorTemplate_iPhone"
                                                                                       ofType:@"css"]
                                              encoding:NSUTF8StringEncoding
                                                 error:nil];
    
    NSString *html = [NSString stringWithFormat:@"<html><head><style type=\"text/css\">%@</style></head><body>%@</body></html>", css, maincontent];
    [self.contentWebView loadHTMLString:html baseURL:nil];
}

- (void)setRelatedMaterialsData:(BARelatedMaterialsData *)relatedMaterialsData
{
    _relatedMaterialsData = relatedMaterialsData;
    if ([self isViewLoaded]) {
        [self populateView];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)linkViewTapped
{
    if (self.relatedMaterialsData.aboutAuthor && self.relatedMaterialsData.aboutAuthor.url && [self.relatedMaterialsData.aboutAuthor.url isEqualToString:@""] == NO) {
        BAWebBrowserViewController *browser = [[BAWebBrowserViewController alloc] initWithNibName:@"BAWebBrowserViewController" bundle:nil];
        browser.url = self.relatedMaterialsData.aboutAuthor.url;
        [self presentViewController:browser animated:YES completion:NULL];
    }
}

@end
